package com.triones.yjj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.triones.yjj.entity.*;
import com.triones.yjj.entity.bo.ActivityListParams;
import com.triones.yjj.entity.bo.ComplainListParams;
import com.triones.yjj.entity.bo.ComplainParams;
import com.triones.yjj.entity.vo.ActivityVO;
import com.triones.yjj.entity.vo.ComplainListVO;
import com.triones.yjj.entity.vo.MemberActionListVO;
import com.triones.yjj.entity.vo.StaffVO;
import com.triones.yjj.mapper.*;
import com.triones.yjj.service.ActivityService;
import com.triones.yjj.service.ComplainService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 活动管理 service 业务实现
 *
 * @Author: triones
 */
@Service
@RequiredArgsConstructor
public class ComplainServiceImpl implements ComplainService {

    private final ComplainMapper complainMapper;

    private final MemberMapper memberMapper;

    private final WxUserMapper wxUserMapper;

    private final StaffMapper staffMapper;

    private final StaffActivityRelateMapper staffActivityRelateMapper;

   @Override
    public Boolean getComplainBySourceId(ComplainParams complainParams) {
        LambdaQueryWrapper<ComplainDO> wrapper = Wrappers.lambdaQuery(ComplainDO.class);
        wrapper.eq(ComplainDO::getActivityId,complainParams.getActivityId());
        wrapper.eq(ComplainDO::getMemberId,complainParams.getMemberId());
        long count =  complainMapper.selectCount(wrapper);
        return count>0;
    }

    @Override
    public Boolean save(ComplainParams complainParams) {
        ComplainDO complainDO = new ComplainDO();
        BeanUtil.copyProperties(complainParams,complainDO);
        complainDO.setHandleType("activity_complain");
        complainDO.setHandleStatus(1);
        int count = complainMapper.insert(complainDO);
        return count>0;
    }

    @Override
    public TableDataInfo<ComplainListVO> pageList(ComplainListParams params, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
        QueryWrapper<ComplainDO> wrapper = Wrappers.query();
        wrapper.eq("a.user_id",loginUser.getUserId());
        wrapper.like(ObjectUtil.isNotEmpty(params.getMobile()),"a.mobile",params.getMobile());
        wrapper.eq(ObjectUtil.isNotNull(params.getActivityId()),"a.activity_id",params.getActivityId());
        wrapper.eq(ObjectUtil.isNotNull(params.getHandleStatus()) && params.getHandleStatus() != 0,"a.handle_status",params.getHandleStatus());
        wrapper.eq(ObjectUtil.isNotEmpty(params.getHandleType()),"a.handle_type",params.getHandleType());
        if (params.getDateRange() != null && params.getDateRange().size() > 0) {
            wrapper.between("a.create_time",params.getDateRange().get(0),params.getDateRange().get(1));
        }
        wrapper.orderByDesc("a.create_time");
        Page<ComplainListVO> result = complainMapper.pageList(pageQuery.build(),wrapper);
        return TableDataInfo.build(result);
    }

    @Override
    public Boolean restoreVisitAuth(Long id) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("当前用户退出登录!");
        }
       ComplainDO complainDO = complainMapper.selectById(id);
        if (complainDO == null) {
            throw new ServiceException("投诉内容不存在");
        }
        /**
         * 检测该投诉者是不是有员工身份
         */
        MemberDO memberDO = memberMapper.selectById(complainDO.getMemberId());
        if (memberDO == null) {
            throw new ServiceException("该投诉客户不存在");
        }
        WxUserDO wxUserDO = wxUserMapper.selectById(memberDO.getWxUserId());
        if (wxUserDO != null) {
            throw new ServiceException("该微信用户不存在");
        }
        LambdaQueryWrapper<StaffDO> wrapper = Wrappers.lambdaQuery(StaffDO.class);
        wrapper.eq(StaffDO::getUserId,loginUser.getUserId())
            .eq(StaffDO::getDeleted,0)
            .eq(StaffDO::getWxUserId,wxUserDO.getId());
        StaffDO staffDO = staffMapper.selectOne(wrapper);
        if (staffDO != null) {
            LambdaQueryWrapper<StaffActivityRelateDO> staffActivityRelateWrapper = Wrappers.lambdaQuery(StaffActivityRelateDO.class);
            staffActivityRelateWrapper.eq(StaffActivityRelateDO::getStaffId,staffDO.getId())
                .eq(StaffActivityRelateDO::getActivityId,complainDO.getActivityId());
            StaffActivityRelateDO relateDO = staffActivityRelateMapper.selectOne(staffActivityRelateWrapper);
            if (relateDO == null) {
                relateDO = new StaffActivityRelateDO();
                relateDO.setStaffId(staffDO.getId());
                relateDO.setActivityId(complainDO.getActivityId());
                staffActivityRelateMapper.insert(relateDO);
            }
        }
        complainDO.setHandleStatus(2);
        complainDO.setHandleTime(DateUtil.now());
        return complainMapper.updateById(complainDO) > 0;
    }

}
